package com.cpd.blog.common.diyCommon;

import com.baomidou.mybatisplus.annotation.*;
import com.cpd.blog.common.constant.Constant;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author: sun_zhaoyan
 * @Date: 2022/05/01/2:39
 */
@Getter
@Setter
@EqualsAndHashCode(exclude = {"deleted", "version"})
@ApiModel(description = "mysql 数据库实体基类")
public class BaseEntity implements Serializable {
    private static final long serialVersionUID = 1214348113647467491L;

    /**
     * 主键
     */
    @ApiModelProperty("主键")
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;

    /**
     * 创建时间
     * create_time
     */
    @ApiModelProperty("记录创建时间")
    @TableField(fill = FieldFill.INSERT)
    @JsonFormat(pattern = Constant.DATE_TIME_FORMAT, timezone = Constant.TIME_ZONE)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime createTime;

    /**
     * 修改时间
     * modify_time
     */
    @ApiModelProperty("记录修改时间")
    @TableField(fill = FieldFill.UPDATE)
    @JsonFormat(pattern = Constant.DATE_TIME_FORMAT, timezone = Constant.TIME_ZONE)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime modifyTime;

    // TODO 需要将此字段替换成 被删除的 id, 以便可以添加唯一索引
    /**
     * 删除标志
     * deleted
     */
    @TableLogic
    @TableField
    @JsonIgnore
    private Boolean deleted;

    /**
     * 乐观锁
     * version
     */
    @Version
    @TableField
    @JsonIgnore
    private Long version;
}
